<?php
/**
 * 供app调用埋点投放的活动信息及规则
 * http://baseAddress/api/app/
 * @author Kenn
 * @todo   未做
 */
namespace App\Http\Controllers\Api\App;

//use App\App\Services\AlipayService;
use App\Models\Activity\Activity;
use App\App\Services\Web\ActivityService;
use Illuminate\Http\Request;
use App\Models\ActivityDelivery\ActivityDelivery;
use App\Models\ActivityPoints\ActivityPointsService;
use App\Models\ActivityPoints\ActivityPoints;
use App\Models\ActivityDeliveryDetail\ActivityDeliveryDetail;

use App\App\Services\Api\PointService;

//use App\Http\Controllers\Api\App\Transformer\ActivityTransformer;
use App\Http\Controllers\Controller;

class ActivityController extends Controller
{



    /**
     * 获取当前时间段的埋点数据
     *
     * @param Request         $request
     * @param ActivityService $activityService
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function point(Request $request, ActivityService $activityService, PointService $pointService)
    {
        //        $alipayService = new AlipayService();
        //
        //        $res = $alipayService->transferAccounts(1);
        //
        //        dd($res);



        $this->validate($request, [
            'key'      => 'required|string',
            'platform' => 'required|integer',
        ], [
            'key.required'      => '埋点key未填写',
            'key.string'        => '埋点key类型错误',
            'platform.required' => '平台类型未填写',
            'platform.integer'  => '平台类型错误',
        ]);

        $key = $request->query->get('key');


        $dataRes = $pointService->get($key);
        $platform = intval($request->query->get('platform'));

        $status_code = 300;
        $timestamp = time();
        $data = array();

        $iTag = false;

        if ($platform < $dataRes['platform']) {
            for ($i =0 ; $i < $dataRes['platform'] ; $i++) {
                if (pow(2, $i-1)==$platform) {
                    $iTag = true;
                    break;
                }
            }
        }

        if (!empty(($dataRes))) {
            if ($dataRes['begin_time'] < $timestamp && $dataRes['end_time'] > $timestamp && $iTag) {
                $status_code = 0;

                unset($dataRes['platform']);//投放平台 1-JS 2-iOS 3-Android
                unset($dataRes['begin_time']);
                unset($dataRes['end_time']);
                unset($dataRes['type']); //投放类型 1-banner 2-启动页 3-信息流
                $data = $dataRes;
            }
        }

        return $res = ['data'=>$data,'code'=>$status_code];

        return $this->respondJson($data);
    }


    /**
     *
     * 供app调用埋点投放的活动信息及规则
     *
     * @param Integer $id 活动ID
     *
     * @return json
     *
     */
    public function delivery($id)
    {
        $delivery = ActivityDelivery::where(['id' => $id]);

        if (!$delivery) {
            return $this->setStatusCode(403)->responseNotFound();
        }

        $delivery = $delivery->with("ActivityDelivery")->with("ActivityPoints")->get();

        if (!$delivery) {
            return $this->setStatusCode(403)->responseNotFound();
        }

        return $this->response(['status' => 'success', 'status_code' => 200, 'data' => $this->activityTransformer->transform($delivery)]);
    }


    /**
     *
     * 进入活动
     *
     * @param Integer $id 活动ID
     *
     * @return json
     *
     */
    public function show($id)
    {
        $Activity = Activity::find($id);
        if (!$Activity) {
            return $this->setStatusCode(403)->responseNotFound();
        }

        return $this->response(['sattus' => 'success', 'data' => $this->activityTransformer->transform($Activity)]);
    }
}
